import time
import requests

host = 'http://192.168.0.161:30210'  # 确保这是正确的base_url
headers={
    "Authorization": "Bearer e3c232d4341856e183f0527206948f8fffdb987d76ee422770fb35f7f73748b8",
    "Accept-Language": "en",
    "app-currency": "USD",
    "platform": "2",
    "channel": "dev",
    "app-version": "1.0.0-dev",
    "app-build": "1",
    "device-brand": "Meizu",
    "deviceId": "2ea3caeddd0803132b62b0efadf1a66b3",
    "os-version": "8.1.0",
    "timestamp": "",
    "request-id": "",
    "signature": "8.1.0",
    "Content-Type": "application/json"
}

def login():
    api_login = "/user/open/login-email-pwd"
    url_login = host + api_login
    data = {
        "email": "Ljiang5@qq.com",
        "password": "afdd0b4ad2ec172c586e2150770fbf9e"
    }
    res = requests.post(url=url_login, headers=headers, json=data).json()
    token = res["data"]["accessToken"]
    headers["Authorization"] = f"Bearer {token}"  # 设置全局headers
    print("登录成功，token已设置")


def query_posts():
    api_path = "/posts/posts/query-posts"
    url = host + api_path  # 使用相同host

    params = {
        "pageNum": 1,
        "pageSize": 10,
        "type": 1,
        "region": "ME_EARTH",
        "publishType": 0
    }
    print("请求头:", headers)  # 调试查看实际发送的headers
    res = requests.get(url, headers=headers, params=params)
    print("状态码:", res.status_code)
    print(res.json())


if __name__ == '__main__':

    old_data = {'headers': {'Content-Type': 'application/json'}, 'json': {'email': 'jiang10@qq.com', 'password': 'afdd0b4ad2ec172c586e2150770fbf9e'}, 'method': 'post', 'url': '${get_base_url(melynk)}/user/open/login-email-pwd'}
    headers = {
        "Accept-Language": "en",
        "app-currency": "USD",
        "platform": "2",
        "channel": "dev",
        "app-version": "1.0.0-dev",
        "app-build": "1",
        "device-brand": "Meizu",
        "deviceId": "2ea3caeddd0803132b62b0efadf1a66b3",
        "os-version": "8.1.0",
        "timestamp": "",
        "request-id": "",
        "signature": "8.1.0",
    }
    old_data['headers'].update(headers)
    # print(old_data)
    start_time = time.time()
    end_time = time.time()
    total_time = end_time-start_time
    print(start_time)
    print(total_time)
    # test_login()
    # test_query_posts()